package com.example.asus.Test;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.asus.db.domain.User;
import com.example.asus.db.gen.UserDao;
import com.example.asus.db.service.GreenDaoManager;
import com.example.asus.mydiary.R;

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener{
    private EditText etuser = null;
    private EditText etpsw = null;
    private Button btncommit = null;
    private User mUser;
    private UserDao userDao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        //初始化控件
        etuser = (EditText)findViewById(R.id.reg_etUserName);
        etpsw = (EditText)findViewById(R.id.reg_etPsw);
        btncommit = (Button)findViewById(R.id.reg_btnCommit);
        btncommit.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.reg_btnCommit:
                insertUser();
                etuser.setText("");
                etpsw.setText("");
                break;
        }
    }

    //添加用户
    private User insertUser(){
        Long id=null;
        String userName = etuser.getText().toString();
        String psw = etpsw.getText().toString();
        System.out.print("Info"+userName+"     "+psw);

        if(userName.equals("")||psw.equals("")){
            Toast.makeText(RegisterActivity.this,"请检查用户名或密码是否为空！",Toast.LENGTH_LONG).show();
        }
        else{
            GreenDaoManager.InitDao(this,"user.db");
            userDao = GreenDaoManager.getDaoSession().getUserDao();
            mUser = new User(id,userName,psw);
            userDao.insert(mUser);
        }
        return mUser;
    }

}
